<template>
  <my-container>
    <vxe-grid
      ref="xGrid"
      border
      resizable
      :radio-config="{highlight:true}"
      :export-config="{
        modes:['current', 'selected', 'all']}"
      stripe
      auto-resize
      show-overflow
      size="small"
      height="auto"
      :seq-config="{startIndex: (tablePage.currentPage - 1) * tablePage.pageSize}"
      :pager-config="tablePage"
      :toolbar="tableToolbar"
      :proxy-config="proxyConfig"
      :loading="listLoading"
      :columns="tableColumn"
      @page-change="pageChange"
    >
      <template v-slot:toolbar_buttons>
        <el-form class="ad-form-query" :inline="true" :model="filter" @submit.native.prevent>
          <el-form-item>
            <el-select v-model="filter.type" filterable placeholder="请选择校准类型" style="width:100%;" @change="changeValue">
              <el-option
                v-for="item in typeList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-model="filter.status" clearable placeholder="请选择状态" style="width:100%;">
              <el-option
                v-for="item in flagList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input
              v-model="filter.name"
              placeholder="计量编号"
              clearable
              @keyup.enter.native="getMachineCheckList"
            >
              <template #prefix>
                <i class="el-input__icon el-icon-search" />
              </template>
            </el-input>
          </el-form-item>
          <el-form-item v-if="checkPermission(['api:quality:machinecheck:getpage'])">
            <el-button icon="el-icon-search" type="primary" @click="getMachineCheckList">查询</el-button>
          </el-form-item>
          <el-form-item v-if="checkPermission(['api:quality:machinecheck:add'])">
            <el-button
              icon="el-icon-plus"
              type="primary"
              @click="onAdd"
            >新增</el-button>
          </el-form-item>
        </el-form>
      </template>
      <template v-slot:status_default="{ row}">
        <!-- row.status==-1?"正常":row.status==0?"校验中":row.status==1?"待确认":row.status==2?"校验失败":"" -->
        <el-tag :type="row.status =='正常'?'success':row.status =='校验中'?'':row.status =='待确认'?'warning':row.status =='校验失败'?'danger':'info' ">{{ row.status }}</el-tag>
      </template>
      <!-- :pager-config="{pageSize: 10}" -->
      <template v-if="checkPermission(['api:quality:machinecheck:update'])" v-slot:operate="{$index, row }">
        <vxe-button v-if="checkPermission(['api:quality:machinecheck:update'])" icon="fa fa-edit" title="编辑" circle @click="onEdit($index,row)" />
        <vxe-button icon="fa fa-list" title="校准历史" circle @click="showList($index,row)" />
        <vxe-button v-if="row.status=='待确认'" icon="fa fa-check" title="校准确认" circle @click="onCommit($index,row)" />
      </template>
    </vxe-grid>

    <vxe-modal v-if="checkPermission(['api:quality:machinecheck:getcommit'])" v-model="modalCommitShow" :loading="submitLoading" title="确认校验结果" width="500" height="530" destroy-on-close resize>
      <vxe-form :data="commitForm" :rules="commitFormRules" title-align="right" title-width="110" @submit="onCommitSubmit">
        <vxe-form-item title="校验报告" span="24" title-align="left" title-width="200px" :title-prefix="{icon: 'fa fa-address-card-o'}" />
        <vxe-form-item title="校准时间" field="calTime" span="24" :item-render="{name: 'ElDatePicker', props: {type: 'datetime', format: 'yyyy-MM-dd'}, attrs:{readonly:true}}" />
        <vxe-form-item title="校验结果描述" field="result" span="24" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入校验结果描述',readonly:true}}" />
        <vxe-form-item title="附件" field="calTime" span="24">
          <template v-slot>
            <el-button size="small" type="primary" @click="onDown">附件下载</el-button>
          </template>
        </vxe-form-item>
        <!-- FilePath -->
        <vxe-form-item title="确认内容" span="24" title-align="left" title-width="200px" :title-prefix="{icon: 'fa fa-address-card-o'}" />
        <vxe-form-item title="确认校准时间" field="confirmCalTime" span="24" :item-render="{name: '$input', props: { type: 'date', placeholder: '请选择日期' }}" />
        <vxe-form-item title="确认结果" field="rs" span="24" :item-render="{name: 'ElSelect', options: resultList,props: { clearable: true, filterable: true }}" />
        <vxe-form-item title="备注" field="remark" span="24" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入备注'}}" />
        <vxe-form-item align="right" span="24">
          <vxe-button @click="commitclose()">取消</vxe-button>
          <vxe-button type="submit" status="primary">确认</vxe-button>
        </vxe-form-item>
      </vxe-form>
    </vxe-modal>
    <transition name="fade-transform" mode="out-in">
      <vxe-modal
        v-if="checkPermission(['api:quality:machinecheck:add'])"
        v-model="modalAddShow"
        :loading="submitLoading"
        title="新增设备校验"
        width="800"
        height="580"
        destroy-on-close
        resize
        @close="closeAddForm"
      >
        <vxe-form ref="addForm" :data="addForm" :rules="addFormRules" title-align="right" title-width="120" @submit="onAddSubmit">
          <!-- <vxe-form-item title="基本信息" span="24" title-align="left" title-width="200px" :title-prefix="{icon: 'fa fa-address-card-o'}" /> -->
          <vxe-form-item title="校验类型" field="calType" span="12" :item-render="{name: 'ElSelect', options: typeList, events: { change: changeValue }}" />
          <vxe-form-item title="计量编号" field="controlNo" span="12" :item-render="{name: 'ElInput', attrs: {placeholder: '请输入计量编号'}}" />
          <vxe-form-item title="设备名称" field="equipmentName" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入设备名称'}}" />
          <vxe-form-item title="设备号" field="equipmentNo" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入设备号'}}" />
          <vxe-form-item title="序列号" field="serialNo" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入序列号'}}" />
          <vxe-form-item title="型号" field="model" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入型号'}}" />
          <vxe-form-item title="精度" field="accuracy" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入精度'}}" />
          <vxe-form-item title="参考文档" field="referenceDoc" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入精度'}}" />
          <vxe-form-item title="使用部门" field="depermentId" span="12" :item-render="{name: 'ElSelect', options: bmList,props: { clearable: true, filterable: true }}" />
          <vxe-form-item title="责任人" field="ownerId" span="12" :item-render="{name: 'ElSelect', options: usrList,props: { clearable: true, filterable: true }}" />
          <vxe-form-item title="计量周期" field="calNum" span="6" :item-render="{name: 'ElInputNumber'}" />
          <vxe-form-item title="单位" field="calInterval" span="6" :item-render="{name: 'ElSelect', options: dw}" />
          <vxe-form-item title="计划校准时间" field="planCalTime" span="12" :item-render="{name: '$input', props: { type: 'date', placeholder: '请选择日期' } }" />
          <vxe-form-item v-if="addForm.calType==0" title="校准设备" field="calibrationEquipment" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入校准设备'}}" />
          <vxe-form-item v-if="addForm.calType==0" title="校准项目" field="controlItem" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入未知字段'}}" />
          <vxe-form-item v-if="addForm.calType==1" title="送检供应商" field="planSupplierId" span="12" :item-render="{name: 'ElSelect', options: gysList,props: { clearable: true, filterable: true }}" />
          <vxe-form-item title="校准要求" field="calibrationReq" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入未知字段'}}" />
          <vxe-form-item title="启用" field="enabled" span="12" :item-render="{name: 'ElSwitch'}" />
          <vxe-form-item title="备注" field="remark" span="24" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入备注'}}" />
          <vxe-form-item align="right" span="24">
            <vxe-button type="reset">重置</vxe-button>
            <vxe-button type="submit" status="primary">提交</vxe-button>
          </vxe-form-item>
        </vxe-form>
        <!-- <template v-slot:footer>
          <vxe-button @click="addclose()">取消</vxe-button>
          <vxe-button type="submit" status="primary" @click="onAddSubmit()">提交</vxe-button>
        </template> -->
      </vxe-modal>
    </transition>
    <vxe-modal
      v-if="checkPermission(['api:quality:machinecheck:update'])"
      v-model="editFormVisible"
      :loading="submitLoading"
      title="编辑设备校验"
      width="800"
      height="580"
      destroy-on-close
      resize
    >
      <vxe-form :data="editForm" :rules="editFormRules" title-align="right" title-width="120" @submit="onEditSubmit">
        <!-- <vxe-form-item title="基本信息" span="24" title-align="left" title-width="200px" :title-prefix="{icon: 'fa fa-address-card-o'}" /> -->
        <vxe-form-item title="校验类型" field="calType" span="12" :item-render="{name: 'ElSelect', options: typeList, events: { change: changeValue }}" />
        <vxe-form-item title="计量编号" field="controlNo" span="12" :item-render="{name: 'ElInput', attrs: {placeholder: '请输入计量编号'}}" />
        <vxe-form-item title="设备名称" field="equipmentName" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入设备名称'}}" />
        <vxe-form-item title="设备号" field="equipmentNo" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入设备号'}}" />
        <vxe-form-item title="序列号" field="serialNo" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入序列号'}}" />
        <vxe-form-item title="型号" field="model" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入型号'}}" />
        <vxe-form-item title="精度" field="accuracy" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入精度'}}" />
        <vxe-form-item title="参考文档" field="referenceDoc" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入精度'}}" />
        <vxe-form-item title="使用部门" field="depermentId" span="12" :item-render="{name: 'ElSelect', options: bmList,props: { clearable: true, filterable: true }}" />
        <vxe-form-item title="责任人" field="ownerId" span="12" :item-render="{name: 'ElSelect', options: usrList,props: { clearable: true, filterable: true }}" />
        <vxe-form-item title="计量周期" field="calNum" span="6" :item-render="{name: 'ElInputNumber'}" />
        <vxe-form-item title="单位" field="calInterval" span="6" :item-render="{name: 'ElSelect', options: dw}" />
        <vxe-form-item title="计划校准时间" field="planCalTime" span="12" :item-render="{name: '$input', props: { type: 'date', placeholder: '请选择日期' }}" />
        <vxe-form-item v-if="editForm.calType==0" title="校准设备" field="calibrationEquipment" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入校准设备'}}" />
        <vxe-form-item v-if="editForm.calType==0" title="校准项目" field="controlItem" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入未知字段'}}" />
        <vxe-form-item v-if="editForm.calType==1" title="送检供应商" field="planSupplierId" span="12" :item-render="{name: 'ElSelect', options: gysList,props: { clearable: true, filterable: true }}" />
        <vxe-form-item title="校准要求" field="calibrationReq" span="12" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入未知字段'}}" />
        <vxe-form-item title="启用" field="enabled" span="12" :item-render="{name: 'ElSwitch'}" />
        <vxe-form-item title="备注" field="remark" span="24" :item-render="{name: 'ElInput',attrs: {placeholder: '请输入备注'}}" />
        <vxe-form-item align="right" span="24">
          <vxe-button @click="editclose()">取消</vxe-button>
          <vxe-button type="submit" status="primary">提交</vxe-button>
        </vxe-form-item>

      </vxe-form>
      <!-- <template v-slot:footer>
        <vxe-button @click="editclose()">取消</vxe-button>
        <vxe-button type="submit" status="primary" @click="onEditSubmit()">提交</vxe-button>
      </template> -->
    </vxe-modal>

    <vxe-modal id="ls" v-model="modalShow" width="700" height="670" min-width="460" min-height="320" resize remember storage transfer>
      <template v-slot:title>
        <span>校准历史</span>
      </template>
      <template>
        <vxe-grid
          ref="mGrid"
          border
          resizable
          stripe
          auto-resize
          show-overflow
          size="small"
          height="auto"
          :export-config="{
            modes:['current', 'selected', 'all']}"
          :sync-resize="modalShow"
          :loading="mloading"
          :toolbar="mtableToolbar"
          :proxy-config="mproxyConfig"
          :pager-config="mtablePage"
          :columns="mtableColumn"
          @page-change="mpageChange"
        >
          <template v-slot:statushis_default="{ row }">
            <el-tag :type="row.status =='待确认'?'warning': row.status =='检验成功'?'success':row.status =='校验失败'?'danger':'info' ">{{ row.status }}</el-tag>
          </template>
          <template v-slot:operate="{$index, row }">
            <vxe-button icon="fa fa-download" title="附件下载" circle @click="download($index,row)" />
          </template>
        </vxe-grid>
      </template>
    </vxe-modal>

  </my-container>
</template>

<script>
import XEUtils from 'xe-utils'
// import { removeXbcWarehouse, batchRemoveXbcWarehouse } from '@/api/warehouse/xbcwarehouse'
import { getxbcLocationList } from '@/api/warehouse/xbclocation'
import { getWlffdwList } from '@/api/admin/dictionary'
import { getAllPage, getAllDetailPage } from '@/api/warehouse/stocklist'
import { getMachineCheck, getMachineCheckListPage, addMachineCheck, editMachineCheck, getCheckReportCommit, commitCheckReport, getCheckHisPage, removeMachineCheck } from '@/api/quality/machinecheck'
import { getUserList } from '@/api/admin/user'
import MyContainer from '@/components/my-container'

export default {
  name: 'Stocklist',
  components: { MyContainer },
  data() {
    return {
      filter: {
        name: '',
        type: 0,
        status: null
      },
      typeList: [
        { label: '内部校验', value: 0 },
        { label: '外部校验', value: 1 }
      ],
      flagList: [
        { label: '正常', value: -1 },
        { label: '校验中', value: 0 },
        { label: '待确认', value: 1 },
        { label: '校验失败', value: 2 }
      ],
      dw: [
        { label: '年', value: 1 },
        { label: '月', value: 2 },
        { label: '周', value: 3 }
      ],
      resultList: [
        { label: '合格', value: 1 },
        { label: '不合格', value: 2 }
      ],
      bmList: [],
      gysList: [],
      usrList: [],
      commitForm: {
        id: null,
        machineCheckId: null,
        calTime: null, // 校验类型  0 内部校验  1 外部校验
        prefix: '',
        result: '', // ControlNo
        rs: 1, // 确认结果
        planCalTime: '', // 设备名称
        confirmCalTime: null,
        filePath: '',
        status: null,
        remark: '' // 备注
      },
      addFormRules: {
        controlNo: [{ required: true, message: '请输入计量编号', trigger: 'blur' }],
        planCalTime: [{ required: true, message: '请输入校验时间', trigger: 'blur' }],
        ownerId: [{ required: true, message: '请输入责任人', trigger: 'blur' }]
      },
      commitFormRules: {
        confirmCalTime: [{ required: true, message: '请输入实际校准时间', trigger: 'blur' }],
        rs: [{ required: true, message: '请输入确认结果', trigger: 'blur' }]
      },
      editFormRules: {
        controlNo: [{ required: true, message: '请输入计量编号', trigger: 'blur' }],
        planCalTime: [{ required: true, message: '请输入校验时间', trigger: 'blur' }],
        ownerId: [{ required: true, message: '请输入责任人', trigger: 'blur' }]
      },
      tableToolbar: {
        custom: true,
        export: true,
        slots: {
          buttons: 'toolbar_buttons'
        }
      },
      mfilter: {
        id: null // 校验id
      },
      mtableToolbar: {
        export: true,
        custom: true
      },
      cvisible: false,
      modalCommitShow: false,
      submitLoading: false,
      modalShow: false,
      roles: [],
      statusList: [
        { name: '激活', value: true },
        { name: '禁用', value: false }
      ],
      xbcTypeList: [], // 物料分类下拉
      xbcLocationList: [], // 库位下拉
      wlffdwList: [], // 物料最小发放单位
      wlffslList: [], // 物料最小发放数量
      khdmList: [], // 客户代码
      ckdwList: [], // 库存单位列表
      mproxyConfig: {
        // seq: true, // 启用动态序号代理
        // sort: true, // 启用排序代理
        // filter: true, // 启用筛选代理
        ajax: {
          // 任何支持 Promise API 的库都可以对接（fetch、jquery、axios、xe-ajax）
          query: async({ page, sort, filters, form }) => {
            // 处理排序条件
            const para = {
              currentPage: this.mtablePage.currentPage,
              pageSize: this.mtablePage.pageSize,
              filter: this.mfilter
            }
            this.mloading = true
            const res = await getCheckHisPage(para)
            this.mloading = false

            if (!res?.success) {
              return
            }
            this.mtablePage.total = res.data.page.total
            return res.data
          },
          // 被某些特殊功能所触发，例如：导出数据 mode=all 时，会触发该方法并对返回的数据进行导出
          queryAll: () => getAllDetailPage({ wlId: this.mfilter.id, kwId: this.mfilter.id
          })
        }
      },
      proxyConfig: {
        seq: true, // 启用动态序号代理
        // sort: true, // 启用排序代理
        // filter: true, // 启用筛选代理
        props: {
          result: 'result',
          total: 'page.total'
        },
        ajax: {
          // 任何支持 Promise API 的库都可以对接（fetch、jquery、axios、xe-ajax）, sort, filters, form
          query: async({ page }) => {
            // 处理排序条件
            const para = {
              currentPage: this.tablePage.currentPage, // page.currentPage
              pageSize: this.tablePage.pageSize, // page.pageSize
              filter: this.filter
            }
            this.listLoading = true
            const res = await getMachineCheckListPage(para)
            this.listLoading = false
            if (!res?.success) {
              return
            }
            this.tablePage.total = res.data.page.total
            // const data = res.data.list
            // data.forEach(d => {
            //   d._loading = false
            // })
            // console.log('ccc', res.data)
            if (!res?.success) {
              return
            }
            return res.data
          },
          // 被某些特殊功能所触发，例如：导出数据 mode=all 时，会触发该方法并对返回的数据进行导出
          queryAll: () => getAllPage({ name: this.filter.name, id: this.filter.xbcTypeId
            // currentPage: 0,
            // pageSize: 0,
            // filter: this.filter
          })
          // async({ page }) => {
          //   // 处理排序条件
          //   const para = {
          //     currentPage: 0,
          //     pageSize: page.pageSize,
          //     filter: this.filter
          //   }
          //   this.listLoading = true
          //   const res = await getXbcmaterrialListPage(para)
          //   this.listLoading = false

          //   if (!res?.success) {
          //     return
          //   }

          //   // this.tablePage.total = res.data.total
          //   // const data = res.data.list
          //   // data.forEach(d => {
          //   //   d._loading = false
          //   // })
          //   return res.data.list
          // }
          // () => XEAjax.get('https://api.xuliangzhan.com:10443/api/pub/all')
        }
      },
      tableColumn: [],
      mtableColumn: [
        { field: 'calTime', title: '校准时间', formatter: this.formatDate },
        { field: 'result', title: '校准结果描述' },
        { field: 'remark', title: '备注' },
        { field: 'calUserName', title: '校准人' },
        { field: 'status', title: '状态', slots: {
        // 对应自定义插槽的名称
          default: 'statushis_default'
        }},
        { title: '操作', width: 100, fixed: 'right', slots: { default: 'operate' }}
      ],
      tablePage: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      mtablePage: {
        total: 0,
        currentPage: 1,
        pageSize: 10
      },
      pager: {},
      listLoading: false,
      mloading: false,
      sels: [], // 列表选中列

      addDialogFormVisible: false,
      editFormVisible: false, // 编辑界面是否显示
      editLoading: false,
      // 编辑界面数据
      editForm: {
        calType: 0, // 校验类型  0 内部校验  1 外部校验
        controlNo: '', // ControlNo
        equipmentName: '', // 设备名称
        equipmentNo: '', // 设备号
        serialNo: '', // 序列号
        model: '', // 型号
        accuracy: '', // 精度
        referenceDoc: '', // 参考文档
        depermentId: null, // 使用部门
        ownerId: null, // 责任人
        calNum: 1, // 生命周期
        calInterval: 1, // 计量周期单位  1年  2月
        planCalTime: null, // 计划校准时间
        calibrationEquipment: '', // 校准设备
        controlItem: '', // 未知字段
        planSupplierId: null, // 计划送检供应商
        calibrationReq: '', // 校准要求/范围
        remark: '', // 备注
        enabled: true
      },
      editFormRef: null,
      modalAddShow: false,
      addFormVisible: false, // 新增界面是否显示
      addLoading: false,
      // 新增界面数据
      addForm: {
        calType: 0, // 校验类型  0 内部校验  1 外部校验
        controlNo: '', // ControlNo
        equipmentName: '', // 设备名称
        equipmentNo: '', // 设备号
        serialNo: '', // 序列号
        model: '', // 型号
        accuracy: '', // 精度
        referenceDoc: '', // 参考文档
        depermentId: null, // 使用部门
        ownerId: null, // 责任人
        calNum: 1, // 生命周期
        calInterval: 1, // 计量周期单位  1年  2月
        planCalTime: null, // 计划校准时间
        calibrationEquipment: '', // 校准设备
        controlItem: '', // 未知字段
        planSupplierId: null, // 计划送检供应商
        calibrationReq: '', // 校准要求/范围
        remark: '', // 备注
        enabled: true
      },
      addFormRef: null,
      deleteLoading: false
    }
  },
  mounted() {
    this.tableColumn = [
      { type: 'radio', width: 50 },
      { type: 'seq', width: 60 },
      { field: 'calTypeName', title: '校验类型' },
      { field: 'controlNo', title: '计量编号', width: 100 },
      { field: 'equipmentName', title: '设备名称' },
      { field: 'equipmentNo', title: '设备号' },
      { field: 'serialNo', title: '序列号' },
      { field: 'model', title: '型号' },
      { field: 'accuracy', title: '精度', visible: false },
      { field: 'referenceDoc', title: '参考文档', visible: false },
      { field: 'depermentName', title: '部门/工序' },
      { field: 'ownerName', title: '责任人' },
      { field: 'calCycle', title: '计量周期' },
      { field: 'planCalTime', title: '计划校准时间', formatter: this.formatDate, width: 100 },
      { field: 'calibrationEquipment', title: '校准设备', visible: this.filter.type === 0 },
      { field: 'controlItem', title: '校准项目', visible: this.filter.type === 0 },
      { field: 'planSupplierName', title: '送检供应商', visible: this.filter.type === 1 },
      { field: 'calibrationReq', title: '校准要求' },
      { field: 'enabled', title: '是否可用', cellRender: { name: 'ElSwitch', props: { disabled: true }}},
      { field: 'remark', title: '备注', visible: false },
      { field: 'status', title: '状态', slots: {
        // 对应自定义插槽的名称
        default: 'status_default'
      }}
    ]
    // console.log('ff')
    if (this.checkPermission(['api:quality:machinecheck:update'])) {
      this.tableColumn.push({ title: '操作', width: 150, slots: { default: 'operate' }})
    }
    // this.$refs.xGrid.commitProxy('reload')
    // this.getXbcmaterrials()
  },
  methods: {
    formatDate({ cellValue }) {
      return XEUtils.toDateString(cellValue, 'yyyy-MM-dd')
    },
    async pageChange({ currentPage, pageSize }) {
      this.tablePage.currentPage = currentPage
      this.tablePage.pageSize = pageSize
      // this.getMachineCheckList()
    },
    async mpageChange({ currentPage, pageSize }) {
      this.mtablePage.currentPage = currentPage
      this.mtablePage.pageSize = pageSize
    },
    // 汇总列表
    async  getMachineCheckList() {
      if (this.$refs.xGrid) {
        this.$nextTick(() => {
          this.$refs.xGrid.commitProxy('reload')
        })
      }

      // this.$refs.xGrid.commitProxy('reload')
    },
    // 编辑
    async onEditSubmit() {
      this.submitLoading = true
      const para = _.cloneDeep(this.editForm)
      const res = await editMachineCheck(para)
      this.submitLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.updateOk'),
        type: 'success'
      })
      // this.$refs['editForm'].resetFields()
      this.editFormVisible = false
      this.getMachineCheckList()
    },
    // 新增验证
    addFormValidate: function() {
      let isValid = false
      this.$refs.addForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    // 新增
    async onAddSubmit() {
      this.submitLoading = true
      const para = _.cloneDeep(this.addForm)
      const res = await addMachineCheck(para)
      this.submitLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.addOk'),
        type: 'success'
      })
      this.modalAddShow = !this.modalAddShow
      this.getMachineCheckList()
    },

    async onCommitSubmit() {
      this.submitLoading = true
      this.commitForm.status = this.commitForm.rs
      const para = _.cloneDeep(this.commitForm)
      const res = await commitCheckReport(para)
      this.submitLoading = false

      if (!res?.success) {
        return
      }

      this.$message({
        message: this.$t('admin.addOk'),
        type: 'success'
      })
      this.modalCommitShow = !this.modalCommitShow
      this.getMachineCheckList()
    },
    async changeValue() {
      this.tableColumn = [
        { type: 'radio', width: 50 },
        { type: 'seq', width: 60 },
        { field: 'calTypeName', title: '校验类型' },
        { field: 'controlNo', title: '计量编号', width: 100 },
        { field: 'equipmentName', title: '设备名称' },
        { field: 'equipmentNo', title: '设备号' },
        { field: 'serialNo', title: '序列号' },
        { field: 'model', title: '型号' },
        { field: 'accuracy', title: '精度', visible: false },
        { field: 'referenceDoc', title: '参考文档', visible: false },
        { field: 'depermentName', title: '所属部门' },
        { field: 'ownerName', title: '责任人' },
        { field: 'calCycle', title: '计量周期' },
        { field: 'planCalTime', title: '计划校准时间', formatter: this.formatDate, width: 100 },
        { field: 'calibrationEquipment', title: '校准设备', visible: this.filter.type === 0 },
        { field: 'controlItem', title: '校准项目', visible: this.filter.type === 0 },
        { field: 'planSupplierName', title: '送检供应商', visible: this.filter.type === 1 },
        { field: 'calibrationReq', title: '校准要求' },
        { field: 'enabled', title: '是否可用', cellRender: { name: 'ElSwitch', props: { disabled: true }}},
        { field: 'remark', title: '备注', visible: false },
        { field: 'status', title: '状态', slots: {
        // 对应自定义插槽的名称
          default: 'status_default'
        }}
      ]
      // console.log('ff')
      if (this.checkPermission(['api:quality:machinecheck:update'])) {
        this.tableColumn.push({ title: '操作', width: 150, slots: { default: 'operate' }})
      }
    },
    onDown() {
      const a = document.createElement('a')
      a.href = this.commitForm.prefix + this.commitForm.filePath
      a.download = '校准文档'
      a.click()
    },
    download(index, row) {
      const a = document.createElement('a')
      a.href = row.filePath
      a.download = '校准文档记录'
      a.click()
    },
    // 显示新增界面
    async onAdd() {
      const loading = this.$loading()
      // this.modalShow = !this.modalShow
      if (this.bmList.length === 0) {
        await this.getWlffdwList('ZL_SYBM')
      }
      if (this.gysList.length === 0) {
        await this.getWlffdwList('PL_SUPER')
      }
      if (this.usrList.length === 0) {
        await this.getUserList()
      }
      loading.close()
      this.modalAddShow = !this.modalAddShow
    },
    async getUserList() {
      const res = await getUserList()
      if (res && res.success) {
        this.usrList = res.data
      }
    },
    async getWlffdwList(id) {
      const res = await getWlffdwList({ dicCode: id })
      if (res && res.success) {
        if (id === 'ZL_SYBM') {
          this.bmList = res.data
        }
        if (id === 'PL_SUPER') {
          this.gysList = res.data
        }
      }
    },
    async showList(index, row) {
      this.mfilter.id = row.id
      this.modalShow = !this.modalShow
      if (this.$refs.mGrid) {
        this.$nextTick(() => {
          this.$refs.mGrid.commitProxy('reload')
        })
      }
    },
    async onCommit(index, row) {
      const loading = this.$loading()
      const res = await getCheckReportCommit({ id: row.id })
      loading.close()
      if (res && res.success) {
        const data = res.data
        this.commitForm = data
        console.log('commit', this.commitForm)
        this.modalCommitShow = true
      }
    },
    // 显示编辑界面
    async onEdit(index, row) {
      // const loading = this.$loading()
      this.editFormVisible = true
      this.submitLoading = true
      if (this.bmList.length === 0) {
        await this.getWlffdwList('ZL_SYBM')
      }
      if (this.gysList.length === 0) {
        await this.getWlffdwList('PL_SUPER')
      }
      if (this.usrList.length === 0) {
        await this.getUserList()
      }
      const res = await getMachineCheck({ id: row.id })
      // loading.close()
      if (res && res.success) {
        const data = res.data
        this.editForm = data
        this.submitLoading = false
      }
    },
    closeEditForm() {
    },
    closeAddForm() {
      this.addForm = {
        calType: 0, // 校验类型  0 内部校验  1 外部校验
        controlNo: '', // ControlNo
        equipmentName: '', // 设备名称
        equipmentNo: '', // 设备号
        serialNo: '', // 序列号
        model: '', // 型号
        accuracy: '', // 精度
        referenceDoc: '', // 参考文档
        depermentId: null, // 使用部门
        ownerId: null, // 责任人
        calNum: 1, // 生命周期
        calInterval: 1, // 计量周期单位  1年  2月
        planCalTime: null, // 计划校准时间
        calibrationEquipment: '', // 校准设备
        controlItem: '', // 未知字段
        planSupplierId: null, // 计划送检供应商
        calibrationReq: '', // 校准要求/范围
        remark: '', // 备注
        enabled: true
      }
    },
    // 编辑验证
    editFormValidate: function() {
      let isValid = false
      this.$refs.editForm.validate(valid => {
        isValid = valid
      })
      return isValid
    },
    commitclose() {
      this.modalCommitShow = false
    },
    addclose() {
      this.modalAddShow = false
      this.closeAddForm()
    },
    editclose() {
      this.editFormVisible = false
    },
    async getxbcLocationList(id) {
      const res = await getxbcLocationList({ id: id })
      if (res && res.success) {
        this.xbcLocationList = res.data
        this.addForm.xbcLocationId = null
        this.editForm.xbcLocationId = null
      }
    },
    async editGetxbcLocationList(id) {
      const res = await getxbcLocationList({ id: id })
      if (res && res.success) {
        this.xbcLocationList = res.data
      }
    },
    selsChange: function(sels) {
      this.sels = sels
    }
  }
}
</script>
